/*
 *  Copyright (c) 2015 The CCP project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a Beijing Speedtong Information Technology Co.,Ltd license
 *  that can be found in the LICENSE file in the root of the web site.
 *
 *   http://www.yuntongxun.com
 *
 *  An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */
package com.monkeygroup.mygoplus.view.home.activity;

import java.io.InvalidClassException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;

import com.baidu.mapapi.SDKInitializer;
import com.monkeygroup.mygoplus.MyGoApplication;
import com.monkeygroup.mygoplus.R;
import com.monkeygroup.mygoplus.base.BaseActivity;
import com.monkeygroup.mygoplus.ecchat.common.CCPAppManager;
import com.monkeygroup.mygoplus.ecchat.common.base.OverflowAdapter;
import com.monkeygroup.mygoplus.ecchat.common.base.OverflowAdapter.OverflowItem;
import com.monkeygroup.mygoplus.ecchat.common.dialog.ECAlertDialog;
import com.monkeygroup.mygoplus.ecchat.common.dialog.ECProgressDialog;
import com.monkeygroup.mygoplus.ecchat.common.utils.CrashHandler;
import com.monkeygroup.mygoplus.ecchat.common.utils.ECNotificationManager;
import com.monkeygroup.mygoplus.ecchat.common.utils.ECPreferenceSettings;
import com.monkeygroup.mygoplus.ecchat.common.utils.ECPreferences;
import com.monkeygroup.mygoplus.ecchat.common.utils.LogUtil;
import com.monkeygroup.mygoplus.ecchat.core.ClientUser;
import com.monkeygroup.mygoplus.ecchat.core.ContactsCache;
import com.monkeygroup.mygoplus.ecchat.storage.ContactSqlManager;
import com.monkeygroup.mygoplus.ecchat.storage.ConversationSqlManager;
import com.monkeygroup.mygoplus.ecchat.storage.GroupNoticeSqlManager;
import com.monkeygroup.mygoplus.ecchat.storage.GroupSqlManager;
import com.monkeygroup.mygoplus.ecchat.storage.IMessageSqlManager;
import com.monkeygroup.mygoplus.ecchat.ui.ActivityTransition;
import com.monkeygroup.mygoplus.ecchat.ui.ConversationListFragment;
import com.monkeygroup.mygoplus.ecchat.ui.SDKCoreHelper;
import com.monkeygroup.mygoplus.ecchat.ui.chatting.ChattingActivity;
import com.monkeygroup.mygoplus.ecchat.ui.chatting.ChattingFragment;
import com.monkeygroup.mygoplus.ecchat.ui.chatting.CustomerServiceHelper;
import com.monkeygroup.mygoplus.ecchat.ui.chatting.GlobalConstant;
import com.monkeygroup.mygoplus.ecchat.ui.chatting.IMChattingHelper;
import com.monkeygroup.mygoplus.ecchat.ui.contact.ContactLogic;
import com.monkeygroup.mygoplus.ecchat.ui.contact.ECContacts;
import com.monkeygroup.mygoplus.ecchat.ui.contact.MobileContactSelectActivity;
import com.monkeygroup.mygoplus.ecchat.ui.group.BaseSearch;
import com.monkeygroup.mygoplus.ecchat.ui.group.CreateGroupActivity;
import com.monkeygroup.mygoplus.ecchat.ui.group.ECDiscussionActivity;
import com.monkeygroup.mygoplus.ecchat.ui.group.GroupNoticeActivity;
import com.monkeygroup.mygoplus.ecchat.ui.interphone.InterPhoneListActivity;
import com.monkeygroup.mygoplus.ecchat.ui.meeting.MeetingListActivity;
import com.monkeygroup.mygoplus.ecchat.ui.settings.SettingPersionInfoActivity;
import com.monkeygroup.mygoplus.ecchat.ui.settings.SettingsActivity;
import com.monkeygroup.mygoplus.ecchat.ui.videomeeting.VideoconferenceConversation;
import com.monkeygroup.mygoplus.view.entrance.activity.EntranceActivity;
import com.monkeygroup.mygoplus.view.home.fragment.ContactFragment;
import com.monkeygroup.mygoplus.view.home.fragment.HomeFragment;
import com.monkeygroup.mygoplus.view.home.fragment.PersonalFragment;
import com.monkeygroup.mygoplus.view.home.fragment.RecommendFragment;
import com.yuntongxun.ecsdk.ECChatManager;
import com.yuntongxun.ecsdk.ECDevice;
import com.yuntongxun.ecsdk.ECError;
import com.yuntongxun.ecsdk.SdkErrorCode;
import com.yuntongxun.ecsdk.im.ECGroup;
import com.yuntongxun.ecsdk.platformtools.ECHandlerHelper;

/**
 * 主界面（消息会话界面、联系人界面、群组界面）
 */
@ActivityTransition(3)
public class MainActivity extends BaseActivity implements
        View.OnClickListener, ConversationListFragment.OnUpdateMsgUnreadCountsListener {

    private static final String TAG = "LauncherActivity";
    /**
     * 当前ECLauncherUI 实例
     */
    public static MainActivity mLauncherUI;

    /**
     * 当前ECLauncherUI实例产生个数
     */
//    public static int mLauncherInstanceCount = 0;

    /**
     * 当前主界面RootView
     */
    public View mLauncherView;

    /**
     * 缓存三个TabView
     */
    private final HashMap<Integer, Fragment> mTabViewCache = new HashMap<Integer, Fragment>();
    private OverflowAdapter.OverflowItem[] mItems;

//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        int pid = android.os.Process.myPid();
//
//        //透明状态栏
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//        Intent intentGroup = new Intent();
//        intentGroup.setAction("com.yuntongxun.ecdemo.inited");
//        sendBroadcast(intentGroup);
////        if (mLauncherUI != null) {
////            LogUtil.i(LogUtil.getLogUtilsTag(LauncherActivity.class),
////                    "finish last LauncherUI");
////            mLauncherUI.finish();
////        }
////		CCPAppManager.addActivityUI(mLauncherUI);
//        mLauncherUI = this;
////        mLauncherInstanceCount++;
//        super.onCreate(savedInstanceState);
////        initLauncherUIView();
////        initWelcome();
////        mOverflowHelper = new OverflowHelper(this);
//        // umeng
////		MobclickAgent.updateOnlineConfig(this);
////		MobclickAgent.setDebugMode(true);
//        // 设置页面默认为竖屏
//        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
////        ECContentObservers.getInstance().initContentObserver();
//    }

    @Override
    public void setContentView() {
        int pid = android.os.Process.myPid();

        Intent intentGroup = new Intent();
        intentGroup.setAction("com.yuntongxun.ecdemo.inited");
        sendBroadcast(intentGroup);
        mLauncherUI = this;
        // 设置页面默认为竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        initLauncherUIView();
    }

//    @Override
//    protected boolean isEnableSwipe() {
//        return false;
//    }

//    private boolean mInit = false;
//
//    private Runnable initRunnable = new Runnable() {
//        @Override
//        public void run() {
//            mInit = false;
//            initLauncherUIView();
//        }
//    };

//    private void initWelcome() {
//
//        Intent intentGroup = new Intent();
//        intentGroup.setAction("com.yuntongxun.ecdemo.inited");
//        sendBroadcast(intentGroup);
//
//        if (!mInit) {
//            mInit = true;
//            setContentView(R.layout.activity_welcome);
//
//
//            // 程序启动开始创建一个splash用来初始化程序基本数据
//            ECHandlerHelper.postDelayedRunnOnUI(initRunnable, 2000);
//        }
//    }

    /**
     * 初始化主界面UI视图
     */
    private void initLauncherUIView() {
//        mLauncherView = getLayoutInflater().inflate(R.layout.activity_main, null);
        setContentView(R.layout.activity_main);

//        mLauncherView.setBackgroundColor(getResources().getColor(R.color.red_btn_color_normal));
//        findViewById(R.id.top_bg_view).setPadding(0, 40, 0, 0);
//        initView();
//        setListener();
//        doEvent();
//        mTabViewInit = true;
//        mCustomViewPager = (CCPCustomViewPager) findViewById(R.id.pager);
//        mCustomViewPager.setOffscreenPageLimit(4);
//
//        if (mLauncherUITabView != null) {
//            mLauncherUITabView.setOnUITabViewClickListener(null);
//            mLauncherUITabView.setVisibility(View.VISIBLE);
//        }
//        mLauncherUITabView = (CCPLauncherUITabView) findViewById(R.id.laucher_tab_top);
//        mCustomViewPager.setSlideEnabled(true);
//        mLauncherViewPagerAdapter = new LauncherViewPagerAdapter(this,
//                mCustomViewPager);
//        mLauncherUITabView
//                .setOnUITabViewClickListener(mLauncherViewPagerAdapter);
//
//        findViewById(R.id.btn_plus).setOnClickListener(this);
//        findViewById(R.id.btn_plus).setOnLongClickListener(this);
//        ctrlViewTab(0);

//        Intent intent = getIntent();
//        if (intent != null && intent.getIntExtra("launcher_from", -1) == 1) {
//            // 检测从登陆界面过来，判断是不是第一次安装使用
//            checkFirstUse();
//        }
//
//        // 如果是登陆过来的
//        doInitAction();
    }

    private void settingPersonInfo() {
        if (isUpSetPersonInfo()) {
            Intent settingAction = new Intent(this, SettingPersionInfoActivity.class);
            settingAction.putExtra("from_regist", true);
            startActivityForResult(settingAction, 0x2a);
        }
    }

    public static boolean isUpSetPersonInfo() {
        ClientUser user = CCPAppManager.getClientUser();
        return (user != null && user.getpVersion() == 0);
    }

    /**
     * 检测离线消息
     */
    private void checkOffineMessage() {
        if (SDKCoreHelper.getConnectState() != ECDevice.ECConnectState.CONNECT_SUCCESS) {
            return;
        }
        ECHandlerHelper handlerHelper = new ECHandlerHelper();
        handlerHelper.postDelayedRunnOnThead(new Runnable() {
            @Override
            public void run() {
                boolean result = IMChattingHelper.isSyncOffline();
                if (!result) {
                    ECHandlerHelper.postRunnOnUI(new Runnable() {
                        @Override
                        public void run() {
                            disPostingLoading();
                        }
                    });
                    IMChattingHelper.checkDownFailMsg();
                }
            }
        }, 1000);
    }

    private boolean isFirstUse() {
        boolean firstUse = ECPreferences.getSharedPreferences().getBoolean(
                ECPreferenceSettings.SETTINGS_FIRST_USE.getId(),
                ((Boolean) ECPreferenceSettings.SETTINGS_FIRST_USE
                        .getDefaultValue()).booleanValue());
        return firstUse;
    }

    private void checkFirstUse() {
        boolean firstUse = isFirstUse();

        // Display the welcome message?
        if (firstUse) {
            if (IMChattingHelper.isSyncOffline()) {
                mPostingdialog = new ECProgressDialog(this,
                        R.string.tab_loading);
                mPostingdialog.setCanceledOnTouchOutside(false);
                mPostingdialog.setCancelable(false);
                mPostingdialog.show();
            }
            // Don't display again this dialog
            try {
                ECPreferences.savePreference(
                        ECPreferenceSettings.SETTINGS_FIRST_USE, Boolean.FALSE,
                        true);
            } catch (Exception e) {
                /** NON BLOCK **/
            }
        }
    }

    /**
     * 根据TabFragment Index 查找Fragment
     *
     * @param tabIndex
     * @return
     */
//    public final BaseFragment getTabView(int tabIndex) {
//        LogUtil.d(LogUtil.getLogUtilsTag(LauncherActivity.class),
//                "get tab index " + tabIndex);
//        if (tabIndex < 0) {
//            return null;
//        }
//
//        if (mTabViewCache.containsKey(Integer.valueOf(tabIndex))) {
//            return (BaseFragment) mTabViewCache.get(Integer.valueOf(tabIndex));
//        }
//
//        BaseFragment mFragment = null;
//        switch (tabIndex) {
//            case TAB_CONVERSATION:
//                mFragment = (TabFragment) Fragment.instantiate(this,
//                        ConversationListFragment.class.getName(), null);
//                break;
//            case TAB_ADDRESS:
//                mFragment = (TabFragment) Fragment
//                        .instantiate(this,
//                                MobileContactActivity.MobileContactFragment.class
//                                        .getName(), null);
//                break;
//            case TAB_GROUP:
//
//
//                mFragment = (TabFragment) Fragment.instantiate(this,
//                        GroupListFragment.class.getName(), null);
//                break;
//            case TAB_DISCUSSION_GROUP:
//
//                mFragment = (TabFragment) Fragment.instantiate(this,
//                        DiscussionListFragment.class.getName(), null);
//                break;
//
//            default:
//                break;
//        }
//
//        if (mFragment != null) {
//            mFragment.setActionBarActivity(this);
//        }
//        mTabViewCache.put(Integer.valueOf(tabIndex), mFragment);
//        return mFragment;
//    }
//
//    /**
//     * 根据提供的子Fragment index 切换到对应的页面
//     *
//     * @param index 子Fragment对应的index
//     */
//    public void ctrlViewTab(int index) {
//
//        LogUtil.d(LogUtil.getLogUtilsTag(LauncherActivity.class),
//                "change tab to " + index + ", cur tab " + mCurrentItemPosition
//                        + ", has init tab " + mTabViewInit
//                        + ", tab cache size " + mTabViewCache.size());
//        if ((!mTabViewInit || index < 0)
//                || (mLauncherViewPagerAdapter != null && index > mLauncherViewPagerAdapter
//                .getCount() - 1)) {
//            return;
//        }
//
//        if (mCurrentItemPosition == index) {
//            return;
//        }
//        mCurrentItemPosition = index;
//
//        if (mLauncherUITabView != null) {
//            mLauncherUITabView.doChangeTabViewDisplay(mCurrentItemPosition);
//        }
//
//        if (mCustomViewPager != null) {
//            mCustomViewPager.setCurrentItem(mCurrentItemPosition, false);
//        }
//
//    }

    /**
     * 根据底层库是否支持voip加载相应的子菜单
     */
    void initOverflowItems() {
        if (mItems == null) {
            if (SDKCoreHelper.getInstance().isSupportMedia()) {
                mItems = new OverflowAdapter.OverflowItem[7];
                mItems[0] = new OverflowAdapter.OverflowItem(getString(R.string.main_plus_inter_phone));
                mItems[1] = new OverflowAdapter.OverflowItem(getString(R.string.main_plus_meeting_voice));
                mItems[2] = new OverflowAdapter.OverflowItem(getString(R.string.main_plus_meeting_video));
                mItems[3] = new OverflowAdapter.OverflowItem(getString(R.string.main_plus_groupchat));
                mItems[4] = new OverflowAdapter.OverflowItem(getString(R.string.main_plus_querygroup));
                mItems[5] = new OverflowAdapter.OverflowItem(getString(R.string.create_discussion));
//				mItems[6] = new OverflowAdapter.OverflowItem( getString(R.string.main_plus_money));
                mItems[6] = new OverflowAdapter.OverflowItem(getString(R.string.main_plus_settings));
//				mItems[7] = new OverflowAdapter.OverflowItem(getString(R.string.main_plus_mcmessage));
            } else {
                mItems = new OverflowAdapter.OverflowItem[4];
                mItems[0] = new OverflowAdapter.OverflowItem(getString(R.string.main_plus_groupchat));
                mItems[1] = new OverflowAdapter.OverflowItem(getString(R.string.main_plus_querygroup));
                mItems[2] = new OverflowAdapter.OverflowItem(getString(R.string.create_discussion));
//				mItems[3] = new OverflowAdapter.OverflowItem( getString(R.string.main_plus_money));
                mItems[3] = new OverflowAdapter.OverflowItem(getString(R.string.main_plus_settings));
//				mItems[4] = new OverflowAdapter.OverflowItem(getString(R.string.main_plus_mcmessage));
            }
        }

    }

    @Override
    public boolean onMenuOpened(int featureId, Menu menu) {
//        controlPlusSubMenu();
        return false;
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        LogUtil.d(LogUtil.getLogUtilsTag(MainActivity.class), " onKeyDown");

        if ((event.getKeyCode() == KeyEvent.KEYCODE_BACK)
                && event.getAction() == KeyEvent.ACTION_UP) {
            // dismiss PlusSubMenuHelper
//            if (mOverflowHelper != null && mOverflowHelper.isOverflowShowing()) {
//                mOverflowHelper.dismiss();
//                return true;
//            }
        }

        // 这里可以进行设置全局性的menu菜单的判断
        if ((event.getKeyCode() == KeyEvent.KEYCODE_BACK)
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            doTaskToBackEvent();
        }

        try {

            return super.dispatchKeyEvent(event);
        } catch (Exception e) {
            LogUtil.e(LogUtil.getLogUtilsTag(MainActivity.class),
                    "dispatch key event catch exception " + e.getMessage());
        }

        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (internalReceiver != null) {
            unregisterReceiver(internalReceiver);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        Intent intentGroup = new Intent();
        intentGroup.setAction("com.yuntongxun.ecdemo.inited");
        sendBroadcast(intentGroup);

    }

    @Override
    protected void onResume() {
        LogUtil.i(LogUtil.getLogUtilsTag(MainActivity.class),
                "onResume start");
        super.onResume();

        Intent intentGroup = new Intent();
        intentGroup.setAction("com.yuntongxun.ecdemo.inited");
        sendBroadcast(intentGroup);

        CrashHandler.getInstance().setContext(this);
        // 统计时长
//		MobclickAgent.onResume(this);


        //退出到主界面之后清空
//        boolean fullExit = ECPreferences.getSharedPreferences().getBoolean(
//                ECPreferenceSettings.SETTINGS_FULLY_EXIT.getId(), false);
//        if (fullExit) {
//            try {
////                ECHandlerHelper.removeCallbacksRunnOnUI(initRunnable);
//                ECPreferences.savePreference(
//                        ECPreferenceSettings.SETTINGS_FULLY_EXIT, false, true);
////                ContactsCache.getInstance().stop();
//                CCPAppManager.setClientUser(null);
//                ECDevice.unInitial();
//                finish();
//
//                android.os.Process.killProcess(android.os.Process.myPid());
//                System.exit(0);
//
//                return;
//            } catch (InvalidClassException e) {
//                e.printStackTrace();
//            }
//        }

        String account = getAutoRegistAccount();
        if (TextUtils.isEmpty(account) || !MyGoApplication.loginFlag) {
//            startActivity(new Intent(this, LoginActivity.class));
//            finish();
            return;
        }
        // 注册第一次登陆同步消息
        registerReceiver(new String[]{
                IMChattingHelper.INTENT_ACTION_SYNC_MESSAGE,
                SDKCoreHelper.ACTION_SDK_CONNECT});
        ClientUser user = new ClientUser("").from(account);
        CCPAppManager.setClientUser(user);
        if (!ContactSqlManager.hasContact(user.getUserId())) {
            ECContacts contacts = new ECContacts();
            contacts.setClientUser(user);
            ContactSqlManager.insertContact(contacts);
        }

        checkFirstUse();
        // 如果是登陆过来的
        doInitAction();

        if (SDKCoreHelper.getConnectState() != ECDevice.ECConnectState.CONNECT_SUCCESS
                && !SDKCoreHelper.isKickOff()) {
            ContactsCache.getInstance().load();

            if (!TextUtils.isEmpty(getAutoRegistAccount())) {
                SDKCoreHelper.init(this);
            }
        } else {
            initLogin();
        }
        // 初始化主界面Tab资源
//        if (!mInit) {
//            initLauncherUIView();
//        }
//
        OnUpdateMsgUnreadCounts();
        getTopContacts();
    }

    public void showDialog() {
        mPostingdialog = new ECProgressDialog(this, "加载中...");
        mPostingdialog.setCanceledOnTouchOutside(false);
        mPostingdialog.show();
    }

    private void initLogin() {
        /**延迟加载界面，不然会崩溃**/
        if (!mConversationFragment.isAdded()) {
            showDialog();
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    initLoginFragment();
                    disPostingLoading();
                }
            }, 1000);
        }
    }

    private void getTopContacts() {

        final ArrayList<String> arrayList = ConversationSqlManager.getInstance().qureyAllSession();
        ECChatManager chatManager = ECDevice.getECChatManager();
        if (chatManager == null) {
            return;
        }
        chatManager.getSessionsOfTop(new ECChatManager.OnGetSessionsOfTopListener() {
            @Override
            public void onGetSessionsOfTopResult(ECError error, String[] sessionsArr) {
                if (error.errorCode == SdkErrorCode.REQUEST_SUCCESS) {
                    for (String item : sessionsArr) {
                        ConversationSqlManager.updateSessionToTop(item, true);
                    }
                    List<String> list = Arrays.asList(sessionsArr);
                    for (String a : arrayList) {
                        if (!list.contains(a)) {
                            ConversationSqlManager.updateSessionToTop(a, false);
                        }
                    }
                }
            }
        });
    }

    public void handlerKickOff(String kickoffText) {
        if (isFinishing()) {
            return;
        }
        ECAlertDialog buildAlert = ECAlertDialog.buildAlert(this, kickoffText,
                getString(R.string.dialog_btn_confim),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ECNotificationManager.getInstance()
                                .forceCancelNotification();
                        restartAPP();
                    }
                });
        buildAlert.setTitle("异地登陆");
        buildAlert.setCanceledOnTouchOutside(false);
        buildAlert.setCancelable(false);
        buildAlert.show();
    }

    public void restartAPP() {
        ECDevice.unInitial();
        Intent intent = new Intent(this, EntranceActivity.class);
        startActivity(intent);
    }

    /**
     * 检查是否需要自动登录
     *
     * @return
     */
    private String getAutoRegistAccount() {
        SharedPreferences sharedPreferences = ECPreferences
                .getSharedPreferences();
        ECPreferenceSettings registAuto = ECPreferenceSettings.SETTINGS_REGIST_AUTO;
        String registAccount = sharedPreferences.getString(registAuto.getId(),
                (String) registAuto.getDefaultValue());
        return registAccount;
    }

//    private void controlPlusSubMenu() {
//        if (mOverflowHelper == null) {
//            return;
//        }
//
//        if (mOverflowHelper.isOverflowShowing()) {
//            mOverflowHelper.dismiss();
//            return;
//        }
//
//        if (mItems == null) {
//            initOverflowItems();
//        }
//
//        mOverflowHelper.setOverflowItems(mItems);
//        mOverflowHelper.setOnOverflowItemClickListener(mOverflowItemCliclListener);
//        mOverflowHelper.showAsDropDown(findViewById(R.id.btn_plus));
//    }

    @Override
    protected void onPause() {
        LogUtil.d(LogUtil.getLogUtilsTag(getClass()), "KEVIN Launcher onPause");
        super.onPause();
        // 友盟统计API
//		MobclickAgent.onPause(this);
    }

    /**
     * 返回隐藏到后台
     */
    public void doTaskToBackEvent() {
        moveTaskToBack(true);

    }

    @Override
    public void OnUpdateMsgUnreadCounts() {
        int unreadCount = IMessageSqlManager.qureyAllSessionUnreadCount();
        int notifyUnreadCount = IMessageSqlManager.getUnNotifyUnreadCount();
        int count = unreadCount;
        if (unreadCount >= notifyUnreadCount) {
            count = unreadCount - notifyUnreadCount;
        }
//        if (mLauncherUITabView != null) {
//            mLauncherUITabView.updateMainTabUnread(count);
//        }
    }

    /**
     * TabView 页面适配器
     *
     * @author 容联•云通讯
     * @version 4.0
     * @date 2014-12-4
     */
//    private class LauncherViewPagerAdapter extends FragmentStatePagerAdapter
//            implements ViewPager.OnPageChangeListener,
//            CCPLauncherUITabView.OnUITabViewClickListener {
//        /**
//         *
//         */
//        private int mClickTabCounts;
//        private GroupListFragment mGroupListFragment;
//        private DiscussionListFragment mDissListFragment;
//
//        private final ViewPager mViewPager;
//        private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
//
//        final class TabInfo {
//            private final String tag;
//            private final Class<?> clss;
//            private final Bundle args;
//
//            TabInfo(String _tag, Class<?> _class, Bundle _args) {
//                tag = _tag;
//                clss = _class;
//                args = _args;
//            }
//        }
//
//        public LauncherViewPagerAdapter(FragmentActivity fm, ViewPager pager) {
//            super(fm.getSupportFragmentManager());
//            mViewPager = pager;
//            mViewPager.setAdapter(this);
//            mViewPager.setOnPageChangeListener(this);
//        }
//
//        public void addTab(String tabSpec, Class<?> clss, Bundle args) {
//            String tag = tabSpec;
//
//            TabInfo info = new TabInfo(tag, clss, args);
//            mTabs.add(info);
//            notifyDataSetChanged();
//        }
//
//        @Override
//        public int getCount() {
//            return 4;
//        }
//
//        @Override
//        public Fragment getItem(int position) {
//            return mLauncherUI.getTabView(position);
//        }
//
//        @Override
//        public void onPageScrollStateChanged(int state) {
//            LogUtil.d(LogUtil.getLogUtilsTag(LauncherViewPagerAdapter.class),
//                    "onPageScrollStateChanged state = " + state);
//
//            if (state != ViewPager.SCROLL_STATE_IDLE
//                    || mGroupListFragment == null) {
//                return;
//            }
//            if (mGroupListFragment != null) {
//                mGroupListFragment.onGroupFragmentVisible(true);
//                mGroupListFragment = null;
//            }
//        }
//
//        @Override
//        public void onPageScrolled(int position, float positionOffset,
//                                   int positionOffsetPixels) {
//            LogUtil.d(LogUtil.getLogUtilsTag(LauncherViewPagerAdapter.class),
//                    "onPageScrolled " + position + " " + positionOffset + " "
//                            + positionOffsetPixels);
//            if (mLauncherUITabView != null) {
//                mLauncherUITabView.doTranslateImageMatrix(position,
//                        positionOffset);
//            }
//            if (positionOffset != 0.0F && position == CCPLauncherUITabView.TAB_VIEW_THIRD) {
//                if (mGroupListFragment == null) {
//                    mGroupListFragment = (GroupListFragment) getTabView(CCPLauncherUITabView.TAB_VIEW_THIRD);
//                    mGroupListFragment.onGroupFragmentVisible(true);
//                }
//            }
//            if (positionOffset != 0.0F && position == CCPLauncherUITabView.TAB_VIEW_FOUR) {
//                if (mDissListFragment == null) {
//                    mDissListFragment = (DiscussionListFragment) getTabView(CCPLauncherUITabView.TAB_VIEW_FOUR);
//                    mDissListFragment.onDisGroupFragmentVisible(true);
//                }
//            }
//
//
//        }
//
//        @Override
//        public void onPageSelected(int position) {
//            LogUtil.d(LogUtil.getLogUtilsTag(LauncherViewPagerAdapter.class),
//                    "onPageSelected");
//            if (mLauncherUITabView != null) {
//                mLauncherUITabView.doChangeTabViewDisplay(position);
//                mCurrentItemPosition = position;
//            }
//        }
//
//        @Override
//        public void onTabClick(int tabIndex) {
//            if (tabIndex == mCurrentItemPosition) {
//                LogUtil.d(
//                        LogUtil.getLogUtilsTag(LauncherViewPagerAdapter.class),
//                        "on click same index " + tabIndex);
//                // Perform a rolling
//                TabFragment item = (TabFragment) getItem(tabIndex);
//                item.onTabFragmentClick();
//                return;
//            }
//
//            mClickTabCounts += mClickTabCounts;
//            LogUtil.d(LogUtil.getLogUtilsTag(LauncherViewPagerAdapter.class),
//                    "onUITabView Click count " + mClickTabCounts);
//            mViewPager.setCurrentItem(tabIndex);
//        }
//
//    }

    /**
     * 网络注册状态改变
     *
     * @param connect
     */
    public void onNetWorkNotify(ECDevice.ECConnectState connect) {

        if (mConversationFragment.isAdded()) {
            mConversationFragment.updateConnectState();
        }
//        BaseFragment tabView = getTabView(TAB_CONVERSATION);
//        if (tabView instanceof ConversationListFragment && tabView.isAdded()) {
//            ((ConversationListFragment) tabView).updateConnectState();
//        }
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.mygo_main_homepage:
                setFragment(mHomeFragment);
                refreshBottomIcon(homepage_icon, getResources().getDrawable(R.mipmap.main_tab_homepage_focus));
                break;
            case R.id.mygo_main_conversation:
                if (!MyGoApplication.loginFlag) {
                    EntranceActivity.gotoActivity(this);
                    return;
                }

                showChatFragment();
                break;
            case R.id.mygo_main_personal:
                if (!MyGoApplication.loginFlag) {
                    EntranceActivity.gotoActivity(this);
                    return;
                }

                setFragment(mPersonalFragment);
                refreshBottomIcon(personal_icon, getResources().getDrawable(R.mipmap.main_tab_personal_focus));
                break;
            default:
                break;
        }

//        if (v.getId() == R.id.btn_plus) {
//            controlPlusSubMenu();
//        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        Intent actionIntent = intent;
        if (GlobalConstant.ACTION_CREATE_ENTER_GROUP.equals(actionIntent.getAction())) {

            Intent enterIntent = new Intent(MainActivity.this, ChattingActivity.class);
            enterIntent.putExtra(ChattingFragment.RECIPIENTS, actionIntent.getStringExtra("id"));
            enterIntent.putExtra(ChattingFragment.CONTACT_USER, actionIntent.getStringExtra("name"));
            enterIntent.putExtra(ChattingFragment.CUSTOMER_SERVICE, false);
            startActivity(enterIntent);
            return;
        }

        if (GlobalConstant.ACTION_ENTER_NEW_CHATROOM.equals(actionIntent.getAction())) {

            Intent enterIntent = new Intent(MainActivity.this, ChattingActivity.class);
            enterIntent.putExtra(ChattingFragment.RECIPIENTS, actionIntent.getStringExtra("userid"));
            startActivity(enterIntent);
            return;
        }

        String userName = actionIntent.getStringExtra("Main_FromUserName");
        if (userName.equals("mygoguanfangid")) {
            return;
        }

        String mSession = actionIntent.getStringExtra("Main_Session");
        ECContacts contacts = ContactSqlManager
                .getContactLikeUsername(userName);
        if (contacts != null) {
            LogUtil.d(LogUtil.getLogUtilsTag(getClass()),
                    "[onNewIntent] userName = " + userName + " , contact_id "
                            + contacts.getContactid());

            if (GroupNoticeSqlManager.CONTACT_ID
                    .equals(contacts.getContactid())) {
                Intent noticeintent = new Intent(this,
                        GroupNoticeActivity.class);
                startActivity(noticeintent);
                return;
            }

            Intent chatIntent = new Intent(this, ChattingActivity.class);
            String recipinets;
            String username;
            if (!TextUtils.isEmpty(mSession) && mSession.startsWith("g")) {
                ECGroup ecGroup = GroupSqlManager.getECGroup(mSession);
                if (ecGroup == null) {
                    return;
                }
                recipinets = mSession;
                username = ecGroup.getName();
            } else {
                recipinets = contacts.getContactid();
                username = contacts.getNickname();
            }
            startActivity(chatIntent);

            CCPAppManager.startChattingAction(this, recipinets, username);
            return;
        }
    }

//    private final AdapterView.OnItemClickListener mOverflowItemCliclListener = new AdapterView.OnItemClickListener() {
//
//        @Override
//        public void onItemClick(AdapterView<?> parent, View view, int position,
//                                long id) {
////            controlPlusSubMenu();
//
//            OverflowItem overflowItem = mItems[position];
//            String title = overflowItem.getTitle();
//
//            if (getString(R.string.main_plus_inter_phone).equals(title)) {
//                // 实时对讲
//                startActivity(new Intent(MainActivity.this,
//                        InterPhoneListActivity.class));
//            } else if (getString(R.string.main_plus_meeting_voice).equals(title)) {
//                // 语音会议
//                startActivity(new Intent(MainActivity.this,
//                        MeetingListActivity.class));
//            } else if (getString(R.string.main_plus_groupchat).equals(title)) {
//                // 创建群组
//                startActivity(new Intent(MainActivity.this, CreateGroupActivity.class));
//            } else if (getString(R.string.main_plus_querygroup).equals(title)) {
//                // 群组搜索
//                startActivity(new Intent(MainActivity.this, BaseSearch.class));
//            } else if (getString(R.string.main_plus_mcmessage).equals(title)) {
//                handleStartServiceEvent();
//
//            } else if (getString(R.string.main_plus_settings).equals(title)) {
//                // 设置;
//                startActivity(new Intent(MainActivity.this, SettingsActivity.class));
//
//
//            } else if (getString(R.string.main_plus_meeting_video).equals(title)) {
//                startActivity(new Intent(MainActivity.this,
//                        VideoconferenceConversation.class));
//
//            } else if (getString(R.string.create_discussion).equals(title)) {
//
//                Intent intent = new Intent(MainActivity.this, MobileContactSelectActivity.class);
//                intent.putExtra("is_discussion", true);
//                intent.putExtra("isFromCreateDiscussion", true);
//                intent.putExtra("group_select_need_result", true);
//                startActivity(intent);
//
//
//            } else if (getString(R.string.query_discussion).equals(title)) {
//
//                Intent intent = new Intent(MainActivity.this, ECDiscussionActivity.class);
//                intent.putExtra("is_discussion", true);
//
//                startActivity(intent);
//
//            } else if (getString(R.string.main_plus_money).equals(title)) {
//
//            }
//        }
//
//    };

//    /**
//     * 在线客服
//     */
//    private void handleStartServiceEvent() {
//        showProcessDialog();
//        CustomerServiceHelper.startService(ContactLogic.CUSTOM_SERVICE,
//                new CustomerServiceHelper.OnStartCustomerServiceListener() {
//                    @Override
//                    public void onError(ECError error) {
//                        dismissPostingDialog();
//                    }
//
//                    @Override
//                    public void onServiceStart(String event) {
//                        dismissPostingDialog();
//                        CCPAppManager.startCustomerServiceAction(
//                                MainActivity.this, event);
//                    }
//                });
//    }

    private InternalReceiver internalReceiver;

    /**
     * 注册广播
     *
     * @param actionArray
     */
    protected final void registerReceiver(String[] actionArray) {
        if (actionArray == null) {
            return;
        }
        IntentFilter intentfilter = new IntentFilter();
        for (String action : actionArray) {
            intentfilter.addAction(action);
        }
        if (internalReceiver == null) {
            internalReceiver = new InternalReceiver();
        }
        registerReceiver(internalReceiver, intentfilter);
    }

    private class InternalReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent == null || TextUtils.isEmpty(intent.getAction())) {
                return;
            }
            LogUtil.d(TAG, "[onReceive] action:" + intent.getAction());
            if (IMChattingHelper.INTENT_ACTION_SYNC_MESSAGE.equals(intent
                    .getAction())) {
                disPostingLoading();
            } else if (SDKCoreHelper.ACTION_SDK_CONNECT.equals(intent
                    .getAction())) {
                doInitAction();
                // tetstMesge();
                if (mConversationFragment.isAdded()) {
                    mConversationFragment.updateConnectState();
                }
//                BaseFragment tabView = getTabView(TAB_CONVERSATION);
//                if (tabView != null
//                        && tabView instanceof ConversationListFragment) {
//                    ((ConversationListFragment) tabView).updateConnectState();
//                }
            } else if (SDKCoreHelper.ACTION_KICK_OFF.equals(intent.getAction())) {
                String kickoffText = intent.getStringExtra("kickoffText");
                handlerKickOff(kickoffText);
            }
        }
    }

    private boolean mInitActionFlag;

    /**
     * 处理一些初始化操作
     */
    private void doInitAction() {
        if (SDKCoreHelper.getConnectState() == ECDevice.ECConnectState.CONNECT_SUCCESS
                && !mInitActionFlag) {

            // 检测当前的版本
//            SDKCoreHelper.SoftUpdate mSoftUpdate = SDKCoreHelper.mSoftUpdate;
//            if (mSoftUpdate != null) {
//                if (DemoUtils.checkUpdater(mSoftUpdate.version)) {
//                    boolean force = mSoftUpdate.force;
//                    showUpdaterTips(mSoftUpdate.desc, force);
//                    if (force) {
//                        return;
//                    }
//                }
//            }


            String account = getAutoRegistAccount();
            if (!TextUtils.isEmpty(account)) {
                ClientUser user = new ClientUser("").from(account);
                CCPAppManager.setClientUser(user);
            }
//            settingPersonInfo();
//            IMChattingHelper.getInstance().getPersonInfo();
            // 检测离线消息
            checkOffineMessage();
            mInitActionFlag = true;
        }
    }


    private void disPostingLoading() {
        if (mPostingdialog != null && mPostingdialog.isShowing()) {
            mPostingdialog.dismiss();
        }
    }

    ECAlertDialog showUpdaterTipsDialog = null;

    private void showUpdaterTips(String updateDesc, final boolean force) {
        if (showUpdaterTipsDialog != null) {
            return;
        }
        String negativeText = getString(force ? R.string.settings_logout : R.string.update_next);
        String msg = getString(R.string.new_update_version);
        if (!TextUtils.isEmpty(updateDesc)) {
            msg = updateDesc;
        }
        showUpdaterTipsDialog = ECAlertDialog.buildAlert(this, msg,
                negativeText, getString(R.string.app_update),
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        showUpdaterTipsDialog = null;
                        if (force) {
                            try {
                                ECPreferences
                                        .savePreference(
                                                ECPreferenceSettings.SETTINGS_FULLY_EXIT,
                                                true, true);
                            } catch (InvalidClassException e) {
                                e.printStackTrace();
                            }
                            restartAPP();
                        }
                    }
                }, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        CCPAppManager.startUpdater(MainActivity.this);
                        // restartAPP();
                        showUpdaterTipsDialog = null;
                    }
                });

        showUpdaterTipsDialog.setTitle(R.string.app_tip);
        showUpdaterTipsDialog.setDismissFalse();
        showUpdaterTipsDialog.setCanceledOnTouchOutside(false);
        showUpdaterTipsDialog.setCancelable(false);
        showUpdaterTipsDialog.show();
    }

    private ECProgressDialog mPostingdialog;

    void showProcessDialog() {
        mPostingdialog = new ECProgressDialog(MainActivity.this,
                R.string.login_posting_submit);
        mPostingdialog.show();
    }

    /**
     * 关闭对话框
     */
    private void dismissPostingDialog() {
        if (mPostingdialog == null || !mPostingdialog.isShowing()) {
            return;
        }
        mPostingdialog.dismiss();
        mPostingdialog = null;
    }

    private int chatFocusIndex;
    private boolean isMessageNotice, isContactNotice;

    public static void gotoActivity(Activity activity) {
        Intent intent = new Intent(activity,
                MainActivity.class);

        activity.startActivity(intent);
    }

    private ImageView homepage_icon, conversation_icon, personal_icon;

    private Fragment currFragment = null;
    private HomeFragment mHomeFragment = new HomeFragment();
    private ConversationListFragment mConversationFragment = new ConversationListFragment();
    private PersonalFragment mPersonalFragment = new PersonalFragment();
    private ContactFragment mContactFragment = new ContactFragment();
    private RecommendFragment mRecommendFragment = new RecommendFragment();

    @Override
    public void initView() {
        homepage_icon = (ImageView) findViewById(R.id.homepage_icon);
        conversation_icon = (ImageView) findViewById(R.id.conversation_icon);
        personal_icon = (ImageView) findViewById(R.id.personal_icon);
    }

    @Override
    public void setListener() {
        findViewById(R.id.mygo_main_homepage).setOnClickListener(this);
        findViewById(R.id.mygo_main_conversation).setOnClickListener(this);
        findViewById(R.id.mygo_main_personal).setOnClickListener(this);
    }

    @Override
    public void doEvent() {
        chatFocusIndex = 0;
//        if (!MyGoApplication.loginFlag) {
        initFragment();
//        } else {
//            initLoginFragment();
//        }
        setFragment(mHomeFragment);
    }

    public boolean isMessageNotice() {
        return isMessageNotice;
    }

    public void setIsMessageNotice(boolean isMessageNotice) {
        this.isMessageNotice = isMessageNotice;
    }

    public boolean isContactNotice() {
        return isContactNotice;
    }

    public void setIsContactNotice(boolean isContactNotice) {
        this.isContactNotice = isContactNotice;
    }

    public int getChatFocusIndex() {
        return chatFocusIndex;
    }

    public void setChatFocusIndex(int chatFocusIndex) {
        this.chatFocusIndex = chatFocusIndex;
    }

    /**
     * 添加首页的Fragment，加tag
     **/
    private void initFragment() {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_container, mHomeFragment, "mHomeFragment")
                .hide(mHomeFragment).commit();
    }

    /**
     * 添加所有的Fragment，加tag
     **/
    private void initLoginFragment() {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_container, mConversationFragment, "mConversationFragment")
                .hide(mConversationFragment)
                .add(R.id.fragment_container, mPersonalFragment, "mPersonalFragment")
                .hide(mPersonalFragment)
                .add(R.id.fragment_container, mContactFragment, "mContactFragment")
                .hide(mContactFragment)
                .add(R.id.fragment_container, mRecommendFragment, "mRecommendFragment")
                .hide(mRecommendFragment)
                .show(mHomeFragment).commit();
    }

    private void refreshBottomIcon(ImageView imageView, Drawable drawable) {
        homepage_icon.setImageDrawable(getResources().getDrawable(R.mipmap.main_tab_homepage));
        conversation_icon.setImageDrawable(getResources().getDrawable(R.mipmap.main_tab_conversation));
        personal_icon.setImageDrawable(getResources().getDrawable(R.mipmap.main_tab_personal));

        imageView.setImageDrawable(drawable);
    }

    public void setFragment(Fragment fragment) {
        if (currFragment == null || currFragment != fragment) {
            if (currFragment != null) {
                getSupportFragmentManager().beginTransaction().hide(currFragment).show(fragment).commit();
            } else {
                getSupportFragmentManager().beginTransaction().show(fragment).commit();
            }

            currFragment = fragment;
        }

    }

    private Fragment getCurrChatFragment() {
        if (chatFocusIndex == 0) {
            return mConversationFragment;
        } else if (chatFocusIndex == 1) {
            return mContactFragment;
        } else {
            return mRecommendFragment;
        }
    }

    private void showChatFragment() {
        if (mConversationFragment.isVisible()
                || mContactFragment.isVisible()
                || mRecommendFragment.isVisible()) {
            return;
        }

        setFragment(getCurrChatFragment());
        refreshBottomIcon(conversation_icon, getResources().getDrawable(R.mipmap.main_tab_conversation_focus));
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        //如果用以下这种做法则不保存状态，再次进来的话会显示默认tab
        //总是执行这句代码来调用父类去保存视图层的状态
        //super.onSaveInstanceState(outState);
    }
}
